package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IActiveService;
import com.younion.common.constants.SysConstants;
import com.younion.model.ActiveDetailResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.ActiveVO;

public class CardActivitysGetConsumer extends AbstractRequestConsumer {
	private IActiveService activeService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getActivity(request, response);
	}

	public String getActivity(HttpServletRequest request,
			HttpServletResponse response) {
		List<ActiveVO> activeVOs = null;
		List<ActiveDetailResult> activeDetailResultList = new ArrayList<ActiveDetailResult>();
		ResultObject resultObject = new ResultObject();
		String cardId = request.getParameter("card_id");
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Map paramMap = new HashMap();
			paramMap.put("cardId", cardId);
			activeVOs = activeService.selectActivitysByCardMap(paramMap);
			String img_url_pre = ConfigHelper.fastGetConfig("interface",
					"img_url_pre", "http://localhost:8080/younonmember/");
			if (activeVOs != null && activeVOs.size() > 0) {
				for (ActiveVO activeVO : activeVOs) {
					ActiveDetailResult activeDetailResult = new ActiveDetailResult();
					activeDetailResult.setId(activeVO.getId().toString());
					activeDetailResult.setPic(img_url_pre
							+ SysConstants.ACTIVE_IMG_PATH + "/"
							+ activeVO.getId() + "/" + activeVO.getPicName());
					activeDetailResult.setTitle(activeVO.getTitle());
					activeDetailResult
							.setDescription(activeVO.getDescription());
					activeDetailResult.setTop(activeVO.getTop().toString());
					activeDetailResult.setStartTime(sdf.format(activeVO
							.getStartTime()));
					activeDetailResult.setEndTime(sdf.format(activeVO
							.getEndTime()));
					activeDetailResult
							.setInstruction(activeVO.getInstruction());
					activeDetailResultList.add(activeDetailResult);
				}
			}
			resultObject.setResult(activeDetailResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public IActiveService getActiveService() {
		return activeService;
	}

	public void setActiveService(IActiveService activeService) {
		this.activeService = activeService;
	}

}
